-- UIItemBatchUse
-- Created by chendh
-- 物品批量使用界面

UIItemBatchUse = class("UIItemBatchUse", function()
    return cc.Layer:create();
end);

-- 传入奖励列表
function UIItemBatchUse.create(title, desc, classId, multiNum)
    return UIItemBatchUse.new(title, desc, classId, multiNum);
end

-- 构造函数
function UIItemBatchUse:ctor(title, desc, classId, multiNum)
    self:setName("UIItemBatchUse");
    local node = cc.CSLoader:createNode("layout/item/ItemBatchUse.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.BONUS_NOTIFY_ZORDER + 1);


    self.title = title
    self.desc = desc;
    self.classId = classId;
    self.multiNum = multiNum;

    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 重绘
function UIItemBatchUse:redraw()
    -- 设置标题
    local titleLabel = findChildByName(self.node, "CT/title");
    TextStyleM.setTitleStyle(titleLabel);
    titleLabel:setString(self.title);

    -- 正文样式
    local descLabel = findChildByName(self.node, "CT/desc");
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    descLabel = applyString(descLabel, self.desc);
    -- 设置文字居中显示
    descLabel:setTextHorizontalAlignment(cc.TEXT_ALIGNMENT_CENTER);

    -- 道具
    local itemNode = findChildByName(self.node, "CT/panel/item");
    local itemIcon = findChildByName(itemNode, "item_icon");
    local numLabel = findChildByName(itemNode, "item_num");

    TextStyleM.setTextStyle(numLabel, TextStyleM.TEXT_SIZE_TINY, TextStyleM.TEXT_COLOR_BEIGE, true, -3);
    numLabel:setString(ItemM.getAmount(ME.user, self.classId));

    itemIcon:loadTexture(getItemIconPath(ItemM.query(self.classId, "icon")));
    itemIcon:setScale(0.8);

    -- 使用多个
    local btnMulti = findChildByName(self.node, "CT/btn_multi");
    TextStyleM.setTextStyle(btnMulti, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnMulti:setTitleText(string.format(getLocStr("btn_text_multi"), self.multiNum));
    btnMulti.num = 5;

    if ItemM.getAmount(ME.user, self.classId) < self.multiNum then
        btnMulti:setBright(false);
        btnMulti:setTouchEnabled(false);
    end

    -- 使用1个
    local btnOne = findChildByName(self.node, "CT/btn_one");
    TextStyleM.setTextStyle(btnOne, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnOne:setTitleText(string.format(getLocStr("btn_text_multi"), 1));
    btnOne.num = 1;

    local bg = findChildByName(self.node, "CT/bg");
    local panel = findChildByName(self.node, "CT/panel");
    -- 自适应所有组件
    AlignM.adaptLayout(bg, AlignM.BG_SIZE_NORMAL,
        { titleLabel, descLabel, panel, btnMulti}, { 35, });

    btnOne:setPositionY(btnMulti:getPositionY());

    AlignM.adaptSubBg(self.node, AlignM.BG_SIZE_NORMAL);
end

-- 注册事件响应
function UIItemBatchUse:registerEventCallback()
    self:registerScriptHandler(function(ev)
        if ev == "exit" then
            cclog("UIItemBatchUse 界面析构清理");
            EventMgr.removeAll("UIItemBatchUse");
        end
    end)
end

-- 注册点击事件
function UIItemBatchUse:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_select");

            UIMgr.getCurrentScene():removeFormByName("UIItemBatchUse");
        end
    end
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    AddBackEventHandler(self, btnBack, onBackClick);

    -- 注册使用按钮点击事件
    local btnOne = findChildByName(self.node, "CT/btn_one");
    local btnMulti = findChildByName(self.node, "CT/btn_multi");
    local function onUseClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_select");

            local num = sender.num;
            PropertyM.fastUse(self.classId, num);

            -- 抛出道具批量使用事件
            EventMgr.fire(event.ITEM_BATCH_USE, { ["class_id"] = self.classId, ["num"] = num, });

            UIMgr.getCurrentScene():removeFormByName("UIItemBatchUse");
        end
    end
    btnOne:addTouchEventListener(onUseClick);
    btnMulti:addTouchEventListener(onUseClick);
end

-- 适配
function UIItemBatchUse:resize()
    local node = self.node;

    -- bg充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

